home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 5 / Test / Main.cpp next >
Encoding:
C/C++ Source or Header  |  2004-05-24  |  3.4 KB  |  101 lines

  1. //-----------------------------------------------------------------------------
  2. // System Includes
  3. //-----------------------------------------------------------------------------
  4. #include <windows.h>
  5.  
  6. //-----------------------------------------------------------------------------
  7. // Engine Includes
  8. //-----------------------------------------------------------------------------
  9. #include "..\Engine\Engine.h"
  10.  
  11. //-----------------------------------------------------------------------------
  12. // Test State Class
  13. //-----------------------------------------------------------------------------
  14. class TestState : public State
  15. {
  16. public:
  17.     //-------------------------------------------------------------------------
  18.     // Allows the state to preform any pre-processing construction.
  19.     //-------------------------------------------------------------------------
  20.     virtual void Load()
  21.     {
  22.         m_font = new Font;
  23.     }
  24.  
  25.     //-------------------------------------------------------------------------
  26.     // Allows the state to preform any post-processing destruction.
  27.     //-------------------------------------------------------------------------
  28.     virtual void Close()
  29.     {
  30.         SAFE_DELETE( m_font );
  31.     }
  32.  
  33.     //-------------------------------------------------------------------------
  34.     // Returns the view setup details for the given frame.
  35.     //-------------------------------------------------------------------------
  36.     virtual void RequestViewer( ViewerSetup *viewer )
  37.     {
  38.         viewer->viewClearFlags = D3DCLEAR_TARGET;
  39.     }
  40.  
  41.     //-------------------------------------------------------------------------
  42.     // Updates the state.
  43.     //-------------------------------------------------------------------------
  44.     virtual void Update( float elapsed )
  45.     {
  46.         // Calculate the frame rate.
  47.         static float frameTime = 1.0f;
  48.         static int frameCount = 0;
  49.         frameTime += elapsed;
  50.         frameCount++;
  51.  
  52.         // Update the fps every second.
  53.         if( frameTime > 1.0f )
  54.         {
  55.             sprintf( m_fps, "%d fps", frameCount );
  56.  
  57.             frameTime = 0.0f;
  58.             frameCount = 0;
  59.         }
  60.     }
  61.  
  62.     //-------------------------------------------------------------------------
  63.     // Renders the state.
  64.     //-------------------------------------------------------------------------
  65.     virtual void Render()
  66.     {
  67.         m_font->Render( "This text is rendered using the new Direct3D device.", 10, 10 );
  68.         m_font->Render( m_fps, 10, 50, D3DCOLOR_COLORVALUE( 1.0f, 1.0f, 0.0f, 1.0f ) );
  69.     }
  70.  
  71. private:
  72.     Font *m_font; // A font used to render text.
  73.     char m_fps[16]; // Stores the frame rate as text to be render.
  74. };
  75.  
  76. //-----------------------------------------------------------------------------
  77. // Application specific state setup.
  78. //-----------------------------------------------------------------------------
  79. void StateSetup()
  80. {
  81.     g_engine->AddState( new TestState, true );
  82. }
  83.  
  84. //-----------------------------------------------------------------------------
  85. // Entry point for the application.
  86. //-----------------------------------------------------------------------------
  87. int WINAPI WinMain( HINSTANCE instance, HINSTANCE prev, LPSTR cmdLine, int cmdShow )
  88. {
  89.     // Create the engine setup structure.
  90.     EngineSetup setup;
  91.     setup.instance = instance;
  92.     setup.name = "Rendering Test";
  93.     setup.totalBackBuffers = 2;
  94.     setup.StateSetup = StateSetup;
  95.  
  96.     // Create the engine (using the setup structure), then run it.
  97.     new Engine( &setup );
  98.     g_engine->Run();
  99.  
  100.     return true;
  101. }